<template>
  <div :style="{ margin: '20px 0' }">
    <a-row>
      <a-col :span="18">
        <a-descriptions :title="$t('all.autotext503')" bordered size="small">
          <a-descriptions-item :label="$t('all.autotext1291')">
            <span>{{ $t('all.autotext9') }}：{{ version.test && version.test.version?'V'+version.test.version:'' }}</span>
            <a-button type="link" v-if="version.test && version.test.configId" @click="handleView({
              id: version.test.configId,
            })"
            >
              {{ $t('all.autotext568') }}
            </a-button>
          </a-descriptions-item>
          <a-descriptions-item :label="$t('all.autotext315')">
            <span>{{ $t('all.autotext9') }}：{{ version.pro && version.pro.version?'V'+version.pro.version:'' }} </span>
            <a-button type="link" v-if="version.pro && version.pro.configId" @click="handleView({
              id: version.pro.configId,
            })"
            >
              {{ $t('all.autotext568') }}
            </a-button>
          </a-descriptions-item>
        </a-descriptions>
      </a-col>
    </a-row>
    <s-table
      ref="versionTable"
      size="default"
      :row-key="record => record.id"
      :columns="versionColumns"
      :data="loadVersionData"
      style="margin-top: 20px;"
      :scroll="{ x: 1400 }"
    >
      <span slot="configVersion" slot-scope="text,record">
        V{{ record.configVersion }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a-space>
          <a-button type="link" @click="handleView(record)" class="link-btn">{{ $t('all.autotext568') }}</a-button>
          <a-button
            type="link"
            @click="handleCopy(record)"
            class="link-btn"
            :disabled="!record.copy"
          >{{ $t('all.autotext1285') }}</a-button>
          <a-button
            type="link"
            @click="handleEdit(record)"
            class="link-btn"
            :disabled="!record.edit"
          >{{ $t('all.autotext919') }}</a-button>
          <a-button
            type="link"
            @click="handleSync(record,1)"
            class="link-btn"
            :disabled="!record.canSyncProd"
          >{{ $t('all.autotext165') }}</a-button>
          <a-button
            type="link"
            @click="handleSync(record,2)"
            class="link-btn"
            :disabled="!record.canSyncTest"
          >{{ $t('all.autotext431') }}</a-button>
        </a-space>
      </span>
    </s-table>
    <!-- 发布记录 -->
    <a-descriptions :title="$t('all.autotext316')" style="margin:20px 0px"></a-descriptions>
    <s-table
      ref="recordTable"
      size="default"
      :row-key="record => record.id"
      :columns="recordColumns"
      :data="loadRecordData"
      :scroll="{ x: 1400 }"
    >
      <span slot="version" slot-scope="text,record">
        V{{ record.version }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button type="link" @click="handleRecordView(record)" class="link-btn">{{ $t('all.autotext568') }}</a-button>
      </span>
    </s-table>
    <a-modal
      :title="$t('all.autotext162')"
      :visible="visible"
      @cancel="visible=false"
      @ok="handleOk"
    >
      <a-form-model :model="formModel" :labelCol="{span:4}" :wrapperCol="{span:20}">
        <a-form-model-item :label="$t('all.autotext983')">
          <a-textarea
            v-model="formModel.content"
            :placeholder="$t('all.autotext486')"
            :maxLength="200"
            :rows="4"
          />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import { getCurrentVersion, getProConfigList, getPublishHistoryList, syncConfig, copyConfig, apiUploadViewConfigLog, apiUploadEditConfigLog } from '@/api/project/config'
import moment from 'moment'
import { uploadLog } from '@/utils/log'

export default {
  name: 'Version',
  data () {
    return {
      pId: '',
      // 配置版本表头
      versionColumns: [
        {
          title: this.$t('all.autotext913'),
          width: '150px',
          dataIndex: 'configVersion',
          scopedSlots: { customRender: 'configVersion' }
        },
        {
          title: this.$t('all.autotext561'),
          // width: '200px',
          dataIndex: 'account'
        },
        {
          title: this.$t('all.autotext624'),
          // width: '350px',
          dataIndex: 'createTime'
        },
        {
          title: this.$t('all.autotext983'),
          // width: '700px',
          dataIndex: 'content'
        },
        {
          title: this.$t('all.autotext187'),
          dataIndex: 'action',
          fixed: 'right',
          width: '420px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 发布记录表头
      recordColumns: [
        {
          title: this.$t('all.autotext1458'),
          width: '200px',
          dataIndex: 'account'
        },
        {
          title: this.$t('all.autotext703'),
          width: '200px',
          dataIndex: 'modifyTime'
        },
        {
          title: this.$t('all.autotext611'),
          width: '400px',
          dataIndex: 'content'
        },
        {
          title: this.$t('all.autotext1390'),
          dataIndex: 'publishType',
          customRender: text => {
            return (+text === 1
              ? this.$t('all.autotext1416')
              : +text === 2
                ? this.$t('all.autotext360')
                : '')
          }
        },
        {
          title: this.$t('all.autotext65'),
          dataIndex: 'version',
          scopedSlots: { customRender: 'version' }
        },
        {
          title: this.$t('all.autotext187'),
          dataIndex: 'action',
          width: '150px',
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadVersionData: parameter => {
        return getProConfigList({ ...parameter, proId: this.pId }).then(res => {
          const { content, ...rest } = res
          return {
            ...rest,
            content: content.map(item => {
              item.createTime = moment(item.createTime).format('YYYY-MM-DD HH:mm:ss')
              return item
            })
          }
        })
      },
      // 加载数据方法 必须为 Promise 对象
      loadRecordData: parameter => {
        return getPublishHistoryList({ ...parameter, proId: this.pId }).then(res => {
          const { content, ...rest } = res
          return {
            ...rest,
            content: content.map(item => {
              item.modifyTime = moment(item.modifyTime).format('YYYY-MM-DD HH:mm:ss')
              return item
            })
          }
        })
      },
      visible: false,
      formModel: {
        content: ''
      },
      version: {
        test: '',
        pro: ''
      },
      currentConfigId: '',
      isCopying: false,
      isSyncing: false
    }
  },
  created () {
    const pro = localStorage.getItem('CURRENT_PROID')
    if (pro) {
      this.pId = parseInt(pro)
      this.$route.query.id = this.pId
    }
    if (this.$route.query.id) {
      this.pId = this.$route.query.id
    }
  },
  mounted () {
    this.getCurrentVersion()
  },
  methods: {
    getCurrentVersion () {
      getCurrentVersion({
        proId: this.pId
      }).then(res => {
        this.version = res
      })
    },
    handleView (record) {
      this.$store.commit('changeActiveKey', 1)
      this.$router.push({ path: '/overview/config/detail', query: { configId: record.id } })
      uploadLog(apiUploadViewConfigLog, {
        proId: this.pId,
        configId: record.id
      })
    },
    handleEdit (record) {
      this.$store.commit('changeActiveKey', 1)
      this.$router.push({ path: '/overview/config/detail', query: { configId: record.id, type: 'edit' } })
      uploadLog(apiUploadEditConfigLog, {
        proId: this.pId,
        configId: record.id
      })
    },
    handleCopy (record) {
      this.currentConfigId = record.id
      this.visible = true
    },
    handleOk () {
      if (this.isCopying) {
        return this.$message.info(this.$t('all.autotext1307'))
      }
      this.isCopying = true
      copyConfig({
        currentId: this.currentConfigId,
        content: this.formModel.content,
        proId: this.pId
      }).then(res => {
        this.$message.success(this.$t('all.autotext1093'))
        this.visible = false
        this.currentConfigId = ''
        this.formModel.content = ''
        this.$refs.versionTable.refresh(1)
      }).finally(() => {
        this.isCopying = false
      })
    },
    handleSync (record, classify) {
      const that = this
      this.$confirm({
        title: classify === 1 ? this.$t('all.autotext941.1') : this.$t('all.autotext941.2'),
        onOk () {
          that.syncSure(record, classify)
        }
      })
    },
    syncSure (record, classify) {
      if (this.isSyncing) {
        return this.$message.info('正在同步，请稍候...')
      }
      this.isSyncing = true
      syncConfig({
        configId: record.id,
        classify: classify
      }).then(res => {
        this.$message.success(classify === 1 ? this.$t('all.autotext632.1') : this.$t('all.autotext632.2'))
        this.getCurrentVersion()
        this.$refs.versionTable.refresh(1)
        this.$refs.recordTable.refresh(1)
      }).finally(() => {
        this.isSyncing = false
      })
    },
    handleRecordView (record) {
      this.$store.commit('changeActiveKey', 1)
      this.$router.push({ path: '/overview/config/detail', query: { configId: record.proConfigId } })
    }
  }
}
</script>

<style scoped>
.link-btn{
  padding: 0;
}
</style>
